<?php
namespace App\Aspect;

use App\Exception\IPException;
use App\Helper\IPBlackListHelper;
use Swoft\Aop\Annotation\Mapping\Aspect;
use Swoft\Aop\Annotation\Mapping\Before;
use Swoft\Aop\Annotation\Mapping\PointBean;
use App\Http\Controller\ClientController;
use Swoft\Bean\BeanFactory;
use Swoft\Console\Router\Module;

/**
 * IP 黑名单检测
 * Class IPBlackListAspect
 * @package App\Aspect
 * @Aspect(order=1)
 *
 * @PointBean(
 *     include={}
 * )
 */
class IPBlackListAspect
{

    /**
     * 在路由执行之前执行
     * @Before()
     */
    public function check(){

        // 检测模块启用状态
        if (! constant(Module::DEFINE_DISABLE_BLACK_LIST)) {
            // 开启 IP 黑名单
            $IP = ip();

            /** @var IPBlackListHelper $IPBlackListHelper */
            $IPBlackListHelper = BeanFactory::getBean("IPBlackList");
            if ($IPBlackListHelper->inBlack($IP)) {
                // 在黑名单  抛出异常
                throw new IPException();
            }
        }

    }

}
